﻿package ui.components.scroll{
	import ui.components.Component;
	import ui.components.Box
	import flash.events.MouseEvent
	import flash.events.Event
	import flash.display.Sprite;
	public class ScrollBar extends Component{
		private var decArrow:ScrollButton;
		private var incArrow:ScrollButton;
		private var scrollbg:Box;
		private var tracker:ScrollTracker;
		
		private var _scrollSize:Number = 1000;
		private var _scrollViewable:Number = 200;
		private var _scrollValue:Number = 0;
		
		private var scrollListener:Function
		private var _mouseY:Number
		private var _mouseX:Number
		
		private var pixelSize:int = 10;
		
		
		function ScrollBar(horizontal:Boolean = false){
			decArrow = new ScrollButton(1,pixelSize,decScroll);
			incArrow = new ScrollButton(3,pixelSize,incScroll);
			scrollbg = new Box().fill(0x000000,1,3)//.border(1,0x999999,1);
			tracker = new ScrollTracker();
			scrollbg.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{
				if(scrollbg.mouseY > tracker.y){
					incScrollJump()
				}else{
					decScrollJump()
				}
			})
			tracker.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{
				_mouseY = tracker.mouseY
				tracker.addEventListener(Event.ENTER_FRAME, handleMove)
				tracker.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void{
					tracker.removeEventListener(Event.ENTER_FRAME, handleMove)
					tracker.removeEventListener(MouseEvent.MOUSE_UP, arguments.callee)
				})
				function handleMove(e:Event):void{
					changeByPx(tracker.mouseY-_mouseY)
					//_mouseY = tracker.mouseY
					if(tracker.upState.parent)tracker.removeEventListener(Event.ENTER_FRAME, handleMove)
				}
				
			})
			
			addChild(scrollbg);
			addChild(tracker);
			addChild(decArrow)
			addChild(incArrow)

			tracker.width = pixelSize
			tracker.x = 0
			redraw();
		}
		
		public function changeByPx(px:Number):void{
			scrollValue += (_scrollSize/innerSize)*px
		}
		
		public function decScroll():void{
			scrollValue = _scrollValue-15
		}

		public function incScroll():void{
			scrollValue = _scrollValue+15
		}
		
		public function decScrollJump():void{
			scrollValue = _scrollValue-_scrollViewable
		}

		public function incScrollJump():void{
			scrollValue = _scrollValue+_scrollViewable
		}

		public function set scrollSize(h:Number):void{
			_scrollSize = h;
			_scrollValue = Math.max(Math.min(_scrollValue,_scrollSize-_scrollViewable),0);
		}
		
		public function set scrollViewable(h:Number):void{
			_scrollViewable = h;
			_scrollValue = Math.max(Math.min(_scrollValue,_scrollSize-_scrollViewable),0);
		}
		
		public function set scrollValue(h:Number):void{
			if(h != _scrollValue){
				_scrollValue = Math.max(Math.min(h,_scrollSize-_scrollViewable),0);
				if(scrollListener != null)scrollListener(_scrollValue)
				redraw()
			}
		}
		
		public function get scrollValue():Number{
			return _scrollValue 
		}
		
		
		public function scroll(target:Function):void{
			scrollListener = target;
		}
		
		private function get innerSize():Number{
			return _height - pixelSize*2
		}
		
		public override function get width():Number{
			return _width
		}
		public override function get height():Number{
			return _height 
		}		
		
		protected override function redraw():void{
			
			tracker.height = Math.max(innerSize * _scrollViewable / _scrollSize,10);
			tracker.y = pixelSize+1 + (_scrollValue / (_scrollSize-_scrollViewable) ) * (innerSize-tracker.height-1)
			
			scrollbg.width = pixelSize
			scrollbg.height = _height;
			incArrow.y = _height-pixelSize;
		}
	}
}